package com.alstudio.ui.module.main.fragment.rank;

import java.util.ArrayList;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.alstudio.app.ALLocalEnv;
import com.alstudio.module.xmpp.request.ALUiXmppRequestCreator;
import com.alstudio.pojo.ALXmppEvent;
import com.alstudio.pojo.module.user.User;
import com.alstudio.ui.module.main.fragment.FragmentUpdateDataEngine;
import com.alstudio.ui.module.main.fragment.discovery.DiscoveryGridItemAdapter;
import com.alstudio.ui.module.main.fragment.discovery.DiscoveryGridItemAdapter.MainGridItemShowType;
import com.alstudio.utils.log.ALLog;
import com.alstudio.view.listview.PullRefreshAndLoadMoreView;
import com.alstudio.view.listview.PullRefreshAndLoadMoreView.RankViewLoadStateListener;
import com.alstudio.view.viewpagerextend.ALViewpagerCreator;
import com.alstudio.view.viewpagerextend.FixedTabsView;
import com.alstudio.view.viewpagerextend.MyViewPager;
import com.loovee.imaohu.R;

public class RankFragment extends Fragment implements FragmentUpdateDataEngine,
		OnPageChangeListener {

	private ArrayList<View> views;
	private MyViewPager pager;
	private FixedTabsView mFixedTabsView;
	private ALViewpagerCreator viewPagerCreator;
	private PullRefreshAndLoadMoreView glamourView;
	private PullRefreshAndLoadMoreView richView;
	private PullRefreshAndLoadMoreView SexyView;

	private DiscoveryGridItemAdapter glamourAdapter;
	private DiscoveryGridItemAdapter richAdapter;
	private DiscoveryGridItemAdapter sexyAdapter;

	private ArrayList<User> glamourUsers = new ArrayList<User>();
	private ArrayList<User> richUsers = new ArrayList<User>();
	private ArrayList<User> sexyUsers = new ArrayList<User>();

	private ArrayList<String> titles = new ArrayList<String>();

	private final int SHOW_TYPE_GLAMOUR = 2;
	private final int SHOW_TYPE_RICH = 1;
	private final int SHOW_TYPE_SEXY = 0;
	private int showType = SHOW_TYPE_SEXY;

	private final int REQUEST_GLAMOUR = 0;
	private final int REQUEST_RICH = 1;
	private final int REQUEST_SEXY = 2;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View v = inflater.inflate(R.layout.base_fixed_tabs_viewpager_layout,
				container, false);

		pager = (MyViewPager) v.findViewById(R.id.pager);
		mFixedTabsView = (FixedTabsView) v.findViewById(R.id.fixed_tabs);
		initPagerContent();
		new Handler().postDelayed(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				invokePageChange();
			}
		}, 10);
		return v;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.support.v4.app.Fragment#onHiddenChanged(boolean)
	 */
	@Override
	public void onHiddenChanged(boolean hidden) {
		// TODO Auto-generated method stub
		super.onHiddenChanged(hidden);
		ALLog.d("什么状态先" + hidden);
		if (!hidden) {
			ALLog.d("要刷新数据");
			invokePageChange();
		}
	}

	private void initPagerContent() {

		// initData();

		glamourAdapter = new DiscoveryGridItemAdapter(glamourUsers);
		glamourAdapter.setIconRes(R.drawable.hot_user_icon_01);
		glamourAdapter.setType(MainGridItemShowType.MAIN_GRID_ITEM_FOR_GALMOUR);
		richAdapter = new DiscoveryGridItemAdapter(richUsers);
		richAdapter.setIconRes(R.drawable.hot_user_icon_02);
		richAdapter.setType(MainGridItemShowType.MAIN_GRID_ITEM_FOR_RICH);
		sexyAdapter = new DiscoveryGridItemAdapter(sexyUsers);
		sexyAdapter.setIconRes(R.drawable.hot_user_icon_03);
		sexyAdapter.setType(MainGridItemShowType.MAIN_GRID_ITEM_FOR_SEXY);

		// 初始化魅力榜
		glamourView = new PullRefreshAndLoadMoreView(getActivity(),
				glamourAdapter);
		// 禁止item点击事件
		glamourView.enbaleItemClickListener(false);
		// 监听魅力榜加载事件回调
		glamourView.setLoadStateListener(glamourRankViewLoadStateListener);
		// 初始化富豪榜
		richView = new PullRefreshAndLoadMoreView(getActivity(), richAdapter);
		// 禁止item点击事件
		richView.enbaleItemClickListener(false);
		// 监听富豪榜加载事件回调
		richView.setLoadStateListener(richRankViewLoadStateListener);
		// 初始化女神榜
		SexyView = new PullRefreshAndLoadMoreView(getActivity(), sexyAdapter);
		// 禁止item点击事件
		SexyView.enbaleItemClickListener(false);
		// 监听女神榜加载事件回调
		SexyView.setLoadStateListener(sexyRankViewLoadStateListener);
		// 加入title
		titles.add(getString(R.string.TxtTitleSexy));
		titles.add(getString(R.string.TxtTitleRich));
		titles.add(getString(R.string.TxtTitleGlamour));
		views = new ArrayList<View>();
		views.add(SexyView);
		views.add(richView);
		views.add(glamourView);
		viewPagerCreator = new ALViewpagerCreator(ALLocalEnv.getLocalEnv()
				.getGlobalLayoutInflater(), titles, views);
		viewPagerCreator.setViewPager(pager);
		viewPagerCreator.setFixedTabsView(mFixedTabsView);
		viewPagerCreator.addOnPageChangeListener(this);
		viewPagerCreator.commit();

	}

	private void initData() {
		for (int i = 0; i < 20; i++) {
			User u = new User();
			u.setNick("alonso" + i);
			u.setSex("male");
			glamourUsers.add(u);
			richUsers.add(u);
			sexyUsers.add(u);
		}
	}

	// 魅力榜滑动加载事件回调
	private RankViewLoadStateListener glamourRankViewLoadStateListener = new RankViewLoadStateListener() {

		@Override
		public void onRefresh(PullRefreshAndLoadMoreView view) {
			// TODO Auto-generated method stub
			ALUiXmppRequestCreator.postALUiXmppGetRankListEvent(view.startIndex
					+ "", view.endIndex + "", REQUEST_GLAMOUR);

		}

		@Override
		public void onLoadMore(PullRefreshAndLoadMoreView view) {
			// TODO Auto-generated method stub
			ALUiXmppRequestCreator.postALUiXmppGetRankListEvent(view.startIndex
					+ "", view.endIndex + "", REQUEST_GLAMOUR);

		}
	};
	// 富豪榜滑动加载事件回调
	private RankViewLoadStateListener richRankViewLoadStateListener = new RankViewLoadStateListener() {

		@Override
		public void onRefresh(PullRefreshAndLoadMoreView view) {
			// TODO Auto-generated method stub
			ALUiXmppRequestCreator.postALUiXmppGetRankListEvent(view.startIndex
					+ "", view.endIndex + "", REQUEST_RICH);

		}

		@Override
		public void onLoadMore(PullRefreshAndLoadMoreView view) {
			// TODO Auto-generated method stub
			ALUiXmppRequestCreator.postALUiXmppGetRankListEvent(view.startIndex
					+ "", view.endIndex + "", REQUEST_RICH);

		}
	};
	// 女神榜滑动加载事件回调
	private RankViewLoadStateListener sexyRankViewLoadStateListener = new RankViewLoadStateListener() {

		@Override
		public void onRefresh(PullRefreshAndLoadMoreView view) {
			// TODO Auto-generated method stub
			ALUiXmppRequestCreator.postALUiXmppGetRankListEvent(view.startIndex
					+ "", view.endIndex + "", REQUEST_SEXY);

		}

		@Override
		public void onLoadMore(PullRefreshAndLoadMoreView view) {
			// TODO Auto-generated method stub
			ALUiXmppRequestCreator.postALUiXmppGetRankListEvent(view.startIndex
					+ "", view.endIndex + "", REQUEST_SEXY);

		}
	};

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.alstudio.ui.module.main.fragment.FragmentUpdateDataEngine#onDataUpdateReq
	 * (boolean, java.lang.Object, boolean)
	 */
	@Override
	public void onDataUpdateReq(boolean success, Object data, boolean hasMore) {
		// TODO Auto-generated method stub

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.alstudio.ui.module.main.fragment.FragmentUpdateDataEngine#onRefreshReq
	 * ()
	 */
	@Override
	public void onRefreshReq() {
		// TODO Auto-generated method stub
		invokePageChange();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.support.v4.view.ViewPager.OnPageChangeListener#
	 * onPageScrollStateChanged(int)
	 */
	@Override
	public void onPageScrollStateChanged(int arg0) {
		// TODO Auto-generated method stub
		ALLog.d("onPageScrolled");
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * android.support.v4.view.ViewPager.OnPageChangeListener#onPageScrolled
	 * (int, float, int)
	 */
	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
		// TODO Auto-generated method stub
		ALLog.d("onPageScrolled");
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * android.support.v4.view.ViewPager.OnPageChangeListener#onPageSelected
	 * (int)
	 */
	@Override
	public void onPageSelected(int arg0) {
		// TODO Auto-generated method stub
		showType = arg0;
		ALLog.d("onPageSelected");

		invokePageChange();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.alstudio.ui.module.main.fragment.FragmentUpdateDataEngine#onDatUpdateReq
	 * (com.alstudio.module.xmpp.protocol.xmpp.data.ALXmppData)
	 */
	@Override
	public void onDataUpdateReq(ALXmppEvent data) {
		// TODO Auto-generated method stub
		boolean success = false;
		boolean hasMore = data.getBoolean();
		if (data.getResponseCode() == ALXmppEvent.RESPONSE_OK) {
			// 数据获取成功
			ALLog.d("刷新排行榜数据成功");
			success = true;
		}
		PullRefreshAndLoadMoreView pullRefreshAndLoadMoreView = null;
		DiscoveryGridItemAdapter adapter = null;
		ArrayList<User> users = null;
		ALLog.d("数据类型" + data.getIntData());
		switch (data.getIntData()) {
		case REQUEST_GLAMOUR:
			pullRefreshAndLoadMoreView = glamourView;
			adapter = glamourAdapter;
			users = glamourUsers;
			break;
		case REQUEST_RICH:
			pullRefreshAndLoadMoreView = richView;
			adapter = richAdapter;
			users = richUsers;
			break;
		case REQUEST_SEXY:
			pullRefreshAndLoadMoreView = SexyView;
			adapter = sexyAdapter;
			users = sexyUsers;
			break;
		}
		invokeOnDataUpdate(success, hasMore, adapter,
				pullRefreshAndLoadMoreView, users, data.getData());
	}

	private void invokeOnDataUpdate(boolean success, boolean hasMore,
			DiscoveryGridItemAdapter adapter,
			PullRefreshAndLoadMoreView pullRefreshAndLoadMoreView,
			ArrayList<User> users, Object data) {
		if (success) {
			pullRefreshAndLoadMoreView.updateLoadMoreState(hasMore);
			@SuppressWarnings("unchecked")
			ArrayList<User> tmps = (ArrayList<User>) data;
			if (tmps.size() > 0) {
				if (pullRefreshAndLoadMoreView.isRefreshing()) {
					users.clear();
				}
				users.addAll(tmps);

				tmps.clear();
				tmps = null;
			}
			adapter.notifyDataSetChanged();
		}
		pullRefreshAndLoadMoreView.onLoadCompleted();
	}

	/**
	 * 处理界面变化
	 */
	private void invokePageChange() {
		switch (showType) {
		case SHOW_TYPE_GLAMOUR:
			if (glamourUsers.size() == 0) {
				ALLog.d("魅力榜为空，开始请求");
				// glamourView.autoRefresh();
				ALUiXmppRequestCreator.postALUiXmppGetRankListEvent(
						ALLocalEnv.LOAD_MORE_START_INDEX + "",
						ALLocalEnv.LOAD_MORE_END_INDEX + "", REQUEST_GLAMOUR);

			}
			break;
		case SHOW_TYPE_RICH:
			if (richUsers.size() == 0) {
				ALLog.d("富豪榜为空，开始请求");
				// richView.autoRefresh();
				ALUiXmppRequestCreator.postALUiXmppGetRankListEvent(
						ALLocalEnv.LOAD_MORE_START_INDEX + "",
						ALLocalEnv.LOAD_MORE_END_INDEX + "", REQUEST_RICH);

			}
			break;
		case SHOW_TYPE_SEXY:
			if (sexyUsers.size() == 0) {
				ALLog.d("女神榜为空，开始请求");
				// SexyView.autoRefresh();
				ALUiXmppRequestCreator.postALUiXmppGetRankListEvent(
						ALLocalEnv.LOAD_MORE_START_INDEX + "",
						ALLocalEnv.LOAD_MORE_END_INDEX + "", REQUEST_SEXY);

			}
			break;
		}
	}

}
